package com.longZ.himalaya.presenters;

import androidx.annotation.Nullable;

import com.longZ.himalaya.interfaces.IRecommendPresenter;
import com.longZ.himalaya.interfaces.IRecommendViewCallback;
import com.longZ.himalaya.utils.Constants;
import com.longZ.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 逻辑层
 */
public class RecommendPresenter implements IRecommendPresenter {
    private static final String TAG = "RecommendPresenter";
    private List<IRecommendViewCallback> mCallback = new ArrayList<>();

    /**
     * 私有化
     */
    private RecommendPresenter() {}

    private static RecommendPresenter sInstance = null;
    /**
     * 获取单例对象  （懒汉式）
     *
     * @return
     */
    public static RecommendPresenter getInstance() {
        if (sInstance == null) {
            synchronized (RecommendPresenter.class) {
                if (sInstance == null) {
                    sInstance = new RecommendPresenter();
                }
            }
        }
        return sInstance;
    }

    /**
     * 获取推荐内容。猜你喜欢
     */
    @Override
    public void getRecommendList() {
        updateLoading();  //加载中
        Map<String, String> map = new HashMap<>();    //封装参数
        map.put(DTransferConstants.LIKE_COUNT, Constants.COUNT_RECOMMEND + ""); //这个参数表示一页返回多少条
        /**
         *  开始获取数据
         */
        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
            /**
             * 成功返回数据
             * @param gussLikeAlbumList
             */
            @Override
            public void onSuccess(@Nullable GussLikeAlbumList gussLikeAlbumList) {
                if (gussLikeAlbumList != null) {
                    List<Album> albumList = gussLikeAlbumList.getAlbumList();
                    handlerRecommendResult(albumList); //数据返回成功，更新UI
                }
            }

            /**
             * 出现错误
             * @param i
             * @param s
             */
            @Override
            public void onError(int i, String s) {
                LogUtil.e(TAG, "error - > " + i);
                LogUtil.e(TAG, "errorMsg - > " + s);
                handlerError();
            }
        });
    }

    /**
     * 网络错误
     */
    private void handlerError() {
        if (mCallback != null) {
            for (IRecommendViewCallback callback : mCallback) {
                callback.onNetworkError();
            }
        }
    }

    /**
     * 通知UI更新
     *
     * @param albumList
     */
    private void handlerRecommendResult(List<Album> albumList) {
        if (albumList != null) {
            if (albumList.size() == 0) {
                for (IRecommendViewCallback callback : mCallback) {
                    callback.onEmpty();
                }
            } else {
                for (IRecommendViewCallback callback : mCallback) {
                    callback.onRecommendListLoaded(albumList);
                }
            }
        }
    }

    /**
     * 加载中
     */
    private void updateLoading() {
        for (IRecommendViewCallback mCallback : mCallback) {
            mCallback.onLoading();
        }
    }

    @Override
    public void registerViewCallback(IRecommendViewCallback callback) {
        if (mCallback != null && !mCallback.contains(callback)) {
            mCallback.add(callback);
        }
    }

    @Override
    public void unRegisterViewCallback(IRecommendViewCallback callback) {

    }
}
